//
//  EPGBookingUtil.m
//  EPGChina
//
//  Created by CreeveLiu on 15/1/15.
//  Copyright (c) 2015年 sumavision. All rights reserved.
//

#import "EPGBookingUtil.h"

@implementation EPGBookingUtil

+ (BOOL)bookProgram:(ProgramModel *)program channel:(NSString *)channelName channelId:(NSString *)channelId
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSince1970:program.st];
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = [NSString stringWithFormat:@"%@:%@ 即将开始播放",channelName ,program.t];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    NSDictionary *infoDict = @{
                               @"channelId":channelId,
                               @"channelName":channelName,
                               @"showTime":program.showTime,
                               @"programName":program.t,
                               };
    localNotification.userInfo = infoDict;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    return YES;
}
+ (BOOL)unbookProgram:(ProgramModel *)program channel:(NSString *)channelName channeId:(NSString *)channelId
{
    NSArray *arrNoti = [[UIApplication sharedApplication] scheduledLocalNotifications];
    for (UILocalNotification *noti in arrNoti) {
        if ([[noti.userInfo objectForKey:@"channelId"] isEqualToString:channelId]) {
            if ([[noti.userInfo objectForKey:@"showTime"] isEqualToString:program.showTime]) {
                [[UIApplication sharedApplication] cancelLocalNotification:noti];
            }
        }
    }
    return YES;
}

+ (BOOL)isEPGBooked:(ProgramModel *)program channelId:(NSString *)channelId localNotifications:(NSArray *)arrNoti
{
    BOOL result = NO;
    for (UILocalNotification *noti in arrNoti) {
        if ([[noti.userInfo objectForKey:@"channelId"] isEqualToString:channelId]) {
            if ([[noti.userInfo objectForKey:@"showTime"] isEqualToString:program.showTime]) {
                result = YES;
            }
        }
    }
    return result;
}

@end
